package com.luxf.base.domainevent.model;

import com.luxf.base.domainevent.constants.SentStatus;
import com.luxf.base.domainevent.convert.DomainEventNoticeConverter;
import com.luxf.base.domainevent.utils.EventNoticeUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.Instant;

/**
 * 发送消息本地消息表
 *
 * @author fast66
 * 2021-05-26 11:31
 **/
@Entity
@Table(name = "message_sent_history")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MessageSentHistory {

    @Id
    @Column(name = "id", length = 32)
    private String id;

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private SentStatus status;

    @Column(name = "create_at")
    private Instant createdDate;

    @Column(name = "last_modified_at")
    private Instant lastModifiedDate;

    @Convert(converter = DomainEventNoticeConverter.class)
    @Column(name = "domain_event_notice", columnDefinition = "text")
    private DomainEventNotice domainEventNotice;

    public static MessageSentHistory createNoneStateMessage(DomainEventNotice domainEventNotice) {
        return MessageSentHistory.builder()
                .id(EventNoticeUtils.getMessageId(domainEventNotice))
                .createdDate(Instant.now())
                .status(SentStatus.NONE)
                .domainEventNotice(domainEventNotice)
                .build();
    }

}
